import { NzSafeAny } from "@/components/sf/type";

export class Tool {
  static deepClone(obj: object) {
    const objClone = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object") {
      for (const key in obj) {
        // eslint-disable-next-line no-prototype-builtins
        if (obj.hasOwnProperty(key)) {
          //判断ojb子元素是否为对象，如果是，递归复制
          if (obj[key] && typeof obj[key] === "object") {
            objClone[key] = Tool.deepClone(obj[key]);
          } else {
            //如果不是，简单复制
            objClone[key] = obj[key];
          }
        }
      }
    }
    return objClone;
  }

  static debounce(fn: NzSafeAny, delay: number) {
    if (!fn) throw new Error("缺少必传参数");
    if (typeof delay != "number") throw new Error("请传入正确格式的时间控制");
    let timerId;
    return function (...args) {
      clearTimeout(timerId);
      timerId = setTimeout(() => {
        fn.apply(this, args);
      }, delay);
    };
  }
}
